package di.ass.view;

import java.util.ArrayList;

import di.ass.database.DatabaseHandler;
import di.ass.database.Formula;
import di.ass.input.AlertsAdd;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
import di.ass.R;

public class ListFormula extends Activity implements ViewFactory,
		OnClickListener {
	private ArrayList<Formula> arrayListFormula;

	private ListFormulaAdapter listFormulaAdapter;
	private Button add, del;
	private ImageButton home;
	private EditText search;
	private TextView statisticTextView, title_formula;
	private Button checkBoxAllFormula, filterAll, filterDone, filterNotDone;
	private boolean state_checkAll = false;
	DatabaseHandler database;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.list_formula);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.formula_title);
		title_formula = (TextView) findViewById(R.id.title_formula);
		add = (Button) findViewById(R.id.add_formula);
		del = (Button) findViewById(R.id.del_formula);
		home = (ImageButton) findViewById(R.id.idHome);
		checkBoxAllFormula = (Button) findViewById(R.id.idCheckFormulaAll);
		statisticTextView = (TextView) findViewById(R.id.statisticFor);
		filterAll = (Button) findViewById(R.id.filterFormulaAll);
		filterDone = (Button) findViewById(R.id.filterFormulaDone);
		filterNotDone = (Button) findViewById(R.id.filterFormulaNotDone);
		search = (EditText)findViewById(R.id.autoSearch);
		
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// state_checkAll=true;
		createAndViewDatabase(0,"");
	}

	public void createAndViewDatabase(final int statistic,String nameFormula) {

		add.setOnClickListener(this);
		del.setOnClickListener(this);
		home.setOnClickListener(this);
		filterAll.setOnClickListener(this);
		filterDone.setOnClickListener(this);
		filterNotDone.setOnClickListener(this);
		arrayListFormula = new ArrayList<Formula>();
		listFormulaAdapter = new ListFormulaAdapter(this,
				R.layout.element_list_formula, arrayListFormula);

		final ListView list = (ListView) findViewById(R.id.listFormula);
		list.setAdapter(listFormulaAdapter);
		database = new DatabaseHandler(this); // Tao ra
												// danh sach
		final Formula[] list_Formula = database.getAllFormulas(statistic,nameFormula);
		String textSum = null;
		String[] textButton = null;
		if (database.getLanguageUse().equals("Việt Nam")) {
			textSum = getString(R.string.textSum_vi);
			title_formula.setText(getResources().getStringArray(
					R.array.title_vi)[1]);
			textButton = getResources().getStringArray(R.array.button_vi);
		} else if (database.getLanguageUse().equals("English")) {
			textSum = getString(R.string.textSum_eng);
			title_formula.setText(getResources().getStringArray(
					R.array.title_eng)[1]);
			textButton = getResources().getStringArray(R.array.button_eng);
		}
		add.setText(textButton[0]);
		del.setText(textButton[1]);
		checkBoxAllFormula.setText(textButton[2]);
		filterAll.setText(textButton[3]);
		filterDone.setText(textButton[4]);
		filterNotDone.setText(textButton[5]);
		if (list_Formula == null)
			statisticTextView.setText(textSum + ": 0");
		else
			statisticTextView.setText(textSum + ": " + list_Formula.length);
		statisticTextView.setGravity(Gravity.CENTER);

		if (!state_checkAll && list.getCount() > 0) {
			checkBoxAllFormula.setBackgroundResource(R.drawable.check_all);
			state_checkAll = true;
		} else {
			checkBoxAllFormula.setBackgroundResource(R.drawable.uncheck_all);
			state_checkAll = false;
		}
		final Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.btn_style);
		checkBoxAllFormula.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkBoxAllFormula.startAnimation(animation);
				if (!state_checkAll && list.getCount() > 0) {
					checkBoxAllFormula
							.setBackgroundResource(R.drawable.check_all);
					state_checkAll = true;
					int count = list.getCount();
					for (int i = 0; i < count; i++) {
						ViewGroup row = (ViewGroup) list.getChildAt(i);
						CheckBox check = (CheckBox) row
								.findViewById(R.id.check_formula);
						check.setChecked(!list_Formula[i].getChecked());
					}
				} else {
					checkBoxAllFormula
							.setBackgroundResource(R.drawable.uncheck_all);
					state_checkAll = false;
					int count = list.getCount();
					for (int i = 0; i < count; i++) {
						ViewGroup row = (ViewGroup) list.getChildAt(i);
						CheckBox check = (CheckBox) row
								.findViewById(R.id.check_formula);
						check.setChecked(false);
					}
				}
			}
		});
		
		if (list_Formula != null)
			for (Formula i : list_Formula) {
				Formula dataFormula = new Formula(i);
				int count = database.getFoodsCount(i.getId(), 1);
				int sum = count + database.getFoodsCount(i.getId(), 0);
				if (sum == 0)
					dataFormula.setFull(-1);
				else if (count == sum)
					dataFormula.setFull(1);
				else
					dataFormula.setFull(0);
				Log.i("nsajf",	dataFormula.getName());
				//nameFormulaSearch[j++]="bjh";
				Log.i("nsajf",	dataFormula.getName());
				
				arrayListFormula.add(dataFormula);
				listFormulaAdapter.notifyDataSetChanged();
			}

		search.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				createAndViewDatabase(statistic,search.getText().toString());
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	public boolean checkTick() {
		int i = 0;
		while (arrayListFormula.size() > i && arrayListFormula.size() > 0) {
			if (arrayListFormula.get(i).getChecked())
				return true;
			i++;
		}
		return false;
	}

	public void deleteChecked() {
		DatabaseHandler db = new DatabaseHandler(this);
		int i = 0;

		if (arrayListFormula.size() <= 0)
			return;
		while (arrayListFormula.size() > i && arrayListFormula.size() > 0) {
			if (arrayListFormula.get(i).getChecked()) {
				db.deleteFormula(arrayListFormula.get(i));
			}
			i++;
		}
		createAndViewDatabase(0,"");
	}

	public void unCheck() {
		int i = 0;
		if (arrayListFormula.size() <= 0)
			return;
		while (arrayListFormula.size() > i && arrayListFormula.size() > 0) {
			if (arrayListFormula.get(i).getChecked()) {
				arrayListFormula.get(i).setChecked(false);
			}
			i++;
		}
		createAndViewDatabase(0,"");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.btn_style);
		switch (v.getId()) {
		case R.id.idHome:
			home.startAnimation(animation);
			this.finish();
			break;
		case R.id.add_formula:
			add.startAnimation(animation);
			AlertDialog alertDialog = AlertsAdd
					.ShowAddFormulaDialog(this, null);
			alertDialog.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					state_checkAll = true;
					createAndViewDatabase(0,"");
				}
			});
			alertDialog.show();
			break;
		case R.id.del_formula:
			del.startAnimation(animation);
			String[] textDel = null;
			if (database.getLanguageUse().equals("Việt Nam")) {
				textDel = getResources().getStringArray(
						R.array.delete_formula_vi);
			} else if (database.getLanguageUse().equals("English")) {
				textDel = getResources().getStringArray(
						R.array.delete_formula_eng);
			}
			if (checkTick()) {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle(textDel[0]);
				builder.setMessage(textDel[1]);
				builder.setPositiveButton(textDel[2],
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								deleteChecked();
							}
						});
				builder.setNeutralButton(textDel[3],
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								unCheck();
							}
						});
				builder.setIcon(R.drawable.info_warn);
				builder.show();
			}
			break;
		case R.id.filterFormulaAll:
			filterAll.startAnimation(animation);
			createAndViewDatabase(0,"");
			break;
		case R.id.filterFormulaDone:
			filterDone.startAnimation(animation);
			createAndViewDatabase(1,"");
			break;
		case R.id.filterFormulaNotDone:
			filterNotDone.startAnimation(animation);
			createAndViewDatabase(-1,"");
			break;
		}
	}

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		TextView t = new TextView(this);
		t.setTextColor(Color.WHITE);
		t.setGravity(Gravity.CENTER_HORIZONTAL);
		t.setTypeface(null, Typeface.BOLD);
		t.setTextSize(13);
		return t;

	}
}